package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import pojo.AppInfo;
import pojo.AppVersion;
import pojo.BackendUser;
import service.AppInfoService;
import service.BackendUserService;
import service.VersionService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/manager")
public class BackendUserController {
    @Resource
    private BackendUserService backendUserService;

    @Resource
    private AppInfoService appInfoService;



    @RequestMapping("/toLogin")
    public String toLogin(){
        return "backendlogin";
    }

    @RequestMapping("/login")
    public String login(HttpSession session, @RequestParam String userCode, @RequestParam String userPassword){
        BackendUser backendUser = backendUserService.login(userCode,userPassword);
        if (backendUser!=null){
            session.setAttribute("userSession",backendUser);
            return "redirect:/manager/toMain";
        } else {
            throw new RuntimeException("用户名或密码不正确");
        }
    }

    @RequestMapping("/toMain")
    public String toMain(){
        return "/backend/main";
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/index.jsp";
    }

    @RequestMapping("/checksave")
    public String checksave(Integer id,Integer status){
        appInfoService.onSale(status,id);
        return "redirect:/app/list";
    }

}
